import pandas as pd
import pylab as plt
import seaborn as sns
import detect_GMM
import detect_dtw
import detect_segment
import detect_dp
import detect_drawtraj
import detect_utils
import detect_dbscan
color=['red','orange','yellow','green','cyan','blue','purple','pink','c','black','brown','crimson']
path = 'E:\\Desktop\\zs\\csv\\3days.csv'
data1 = pd.read_csv(path)
data1["traj_segment"] = 0
data1["cluster_id"] = -1
# print(data1)
# print("data1************************************************************************************************")
data2 = pd.read_csv(path)
data2["traj_segment"] = 0
shipdata = detect_segment.traj_segment(data2)  ##先对原始数据进行轨迹分段
data2 = detect_dp.dp(shipdata)  ##对分段后的轨迹进行轨迹点压缩
# print(data2)
# print("data2************************************************************************************************")
MMSIDATA = data2['mmsi']
SHIPSMMSI = list(set(MMSIDATA))
shipcount = len(SHIPSMMSI)
trajlist = list()  ##用列表存放每个轨迹段
for i in range(shipcount):
    mmsi = SHIPSMMSI[i]
    ONESHIP = data2.iloc[:, :][data2[data2.T.index[0]] == mmsi]  # 找出这艘船
    TRAJ_SEGMENT = list(set(ONESHIP["traj_segment"]))  # 这艘船轨迹段的set集合 [0,1,2,3]
    trajcount = len(TRAJ_SEGMENT)  # 这艘船轨迹段的数量 4
    for j in (TRAJ_SEGMENT):
        ONETRAJ = ONESHIP.iloc[:, :][ONESHIP[ONESHIP.T.index[6]] == j]
        trajlist.append(detect_utils.dftolist(ONETRAJ))
# print(trajlist)
# print("************************************************************")
# for i in range(len(trajlist)):
#     for j in range(i+1,len(trajlist)):
#         print(trajlist[i][0][0],"船舶的第",trajlist[i][0][7],"条轨迹和",trajlist[j][0][0],"船舶的第",trajlist[j][0][7],"条轨迹的差距为：",round(detect_dtw.dtw(trajlist[i],trajlist[j]),1))
# 3days
eps = 2.5
min_Pts = 3

# 4ships
# eps = 8
# min_Pts = 2
finaldata = detect_dbscan.DBSCAN(trajlist, eps, min_Pts)
# print(finaldata)

MMSIfinaldata = finaldata['mmsi']
finaldatammsi = list(set(MMSIfinaldata))  # 5艘船
finaldatashipcount = len(finaldatammsi)
print(data1)
for i in range(finaldatashipcount):
    mmsi = SHIPSMMSI[i]
    ONESHIP = finaldata.iloc[:, :][finaldata[finaldata.T.index[0]] == mmsi]  # 找出这艘船[230]
    ONESHIP_DATA1 = data1.iloc[:, :][data1[data1.T.index[0]] == mmsi]  # Y原始数据【230】
    indexvalues = ONESHIP_DATA1.index.values  # data1中每艘船的索引值
    TRAJ_SEGMENT = list(set(ONESHIP["traj_segment"]))  # 这艘船轨迹段的set集合 [0]
    for j in TRAJ_SEGMENT:
        ONETRAJ = ONESHIP.iloc[:, :][ONESHIP[ONESHIP.T.index[6]] == j]
        # print(ONETRAJ)
        # print("*********************************************")
        BEGINTIME = ONETRAJ.iloc[-1]["Unixtime"]
        ENDTIME = ONETRAJ.iloc[0]["Unixtime"]
        cid = ONETRAJ.iloc[-1]["cluster_id"]
        for k in indexvalues:
            if BEGINTIME <= data1.iloc[k]["Unixtime"] <= ENDTIME:
                data1.loc[data1.index == k, 'cluster_id'] = cid
                data1.loc[data1.index == k, 'traj_segment'] = j
print(data1)
cluster_set = set(data1['cluster_id'])
cluster_set.remove(-1)
for clusterid in range(len(cluster_set)):
    ONECLUSTER = data1.iloc[:, :][data1[data1.T.index[7]] == clusterid]  # 对于这条轨迹
    MMSIDATA = ONECLUSTER['mmsi']
    SHIPSMMSI = list(set(MMSIDATA))
    shipcount = len(SHIPSMMSI)
    trajlist = list()  ##用列表存放每个轨迹段
    x_axis_data = []
    y_axis_data = []
    for i in range(shipcount):
        mmsi = SHIPSMMSI[i]
        ONESHIP = ONECLUSTER.iloc[:, :][ONECLUSTER[ONECLUSTER.T.index[0]] == mmsi]  # 找出这艘船
        TRAJ_SEGMENT = list(set(ONESHIP["traj_segment"]))  # 这艘船轨迹段的set集合 [0,1,2,3]
        trajcount = len(TRAJ_SEGMENT)  # 这艘船轨迹段的数量 4
        for j in (TRAJ_SEGMENT):
            ONETRAJ = ONESHIP.iloc[:, :][ONESHIP[ONESHIP.T.index[6]] == j]
            print(ONETRAJ)
            print("************************************************************")
            x = ONETRAJ['longitude']
            y = ONETRAJ['latitude']
            plt.plot(x, y, linewidth=0.5,color = color[clusterid])
    detect_GMM.GMM_xy(ONECLUSTER)
    plt.show()
print("簇的个数为：",len(cluster_set)-1)

